package com.cskaoyan.controller.ControllerByshj;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.order.Comment;
import com.cskaoyan.bean.order.CommentExample;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.wx.comment.bo.CommentListBo;
import com.cskaoyan.bean.wx.comment.vo.CommentVo;
import com.cskaoyan.bean.wx.comment.vo.UserInfoVo;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.CommentService;
import com.cskaoyan.service.WeChartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 主要负责评论，专题，和搜索三部分微信的控制模块
 * 创建日期: 2022/05/07 11:28
 *
 * @author 孙豪骏
 */

@Controller
@RequestMapping("wx")
public class WeChartController {

    @Autowired
    WeChartService weChartService;
    @Autowired
    CommentService commentService;


    //小程序搜索框
    @ResponseBody
    @RequestMapping("search/helper")
    public BaseRespVo wxSearchHelper(String keyword) {
        String[] strings = new String[1];
        strings[0] = keyword;
        WeChartSearchHistory weChatSearchHistory = new WeChartSearchHistory();
        weChatSearchHistory.setKeyword(keyword);

        //TODO：这里有个逻辑歧义    没有界定什么是热门的，可以在后台去更改热门，而是默认
        weChartService.insertSearchHistory(weChatSearchHistory);
        return BaseRespVo.getResult(strings, "成功", 0);
    }

    @ResponseBody
    @RequestMapping("search/index")
    public BaseRespVo wxSearchHelper() {

        List<WeChartSearchHistory> hotSearchHistory = weChartService.queryHotSearchHistory();
        List<WeChartSearchHistory> normalHistory = weChartService.queryNormalHistory();
//默认hotSearch 的第一个为Default
        weChartIndexSearchHistoryList weChartIndexSearchHistoryList = new weChartIndexSearchHistoryList(hotSearchHistory.get(0), hotSearchHistory, normalHistory);
        return BaseRespVo.getResult(weChartIndexSearchHistoryList, "成功", 0);
    }

    //清空历史搜索数据
    @ResponseBody
    @RequestMapping("search/clearhistory")
    public BaseRespVo clearHistory() {
//将整张表都deleted了
        weChartService.clearHistory();
        return BaseRespVo.getResult(null, "成功", 0);
    }


    //展示专题表
    //数据来源market_topic
    //todo 这个方法，我自己没有办法测试，需要数据传入，但是POSTMAN响应看了应该是完美的
    @ResponseBody
    @GetMapping("topic/list")
    public BaseRespVo topicList(BaseParam baseParam) {
        //     PageHelper.startPage(baseParam);
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketTopic> marketTopicList = weChartService.queryTopicList();
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopicList);
        TotalPageLimit<List> totalPageLimit = new TotalPageLimit<List>((int) marketTopicPageInfo.getTotal(), marketTopicPageInfo.getPages(), baseParam.getLimit(), baseParam.getPage(), marketTopicList);
        BaseRespVo result = BaseRespVo.getResult(totalPageLimit, "成功", 0);
        return result;
    }

    //专题中某一个产品的细节展示
    @ResponseBody
    @GetMapping("topic/detail")
    public BaseRespVo topicDetail(Integer id) {
        //todo 在这里面有个goods是什么类型的问题，没有具体处理清楚，给的信息太少，String目前没错，所以目前没有考虑
        MarketTopic marketTopic = weChartService.queryTopicDetail(id);
        MarketTopicDetailVo marketTopicDetailVo = new MarketTopicDetailVo(marketTopic, marketTopic.getGoods());
        return BaseRespVo.getResult(marketTopicDetailVo, "成功", 0);
    }

    //展示有关联的商品。这里我对关联的逻辑定义是id>这个id ，根据参数确定选3确定个,他自己的业也大概是这么设计的
    @ResponseBody
    @GetMapping("topic/related")
    public BaseRespVo topiRelated(Integer id) {
        //想要选择三个相关的，可以直接修改这个变成你想要的相关的
        Integer limit = 3;
        PageHelper.offsetPage(0, limit);
        List<MarketTopic> marketTopicList = weChartService.queryRelatedTopic(id);
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopicList);
        TotalPageLimit<List> listTotalPageLimit = new TotalPageLimit<>((int) marketTopicPageInfo.getTotal(), marketTopicPageInfo.getPages(), limit, marketTopicPageInfo.getPageNum(), marketTopicList);
        return BaseRespVo.getResult(listTotalPageLimit, "成功", 0);
    }


    //得到评论的信息
    //todo： 应该是需要用户信息登陆注册，不知道是不是需要我来判断
    //这又两个传参showType和tyoeid不知道有啥用等后续
    @ResponseBody
    @GetMapping("comment/list")
    public BaseRespVo commentList(CommentListBo commentListBo) {
        //Integer valueId, BaseParam baseParam
        return commentService.getAllCommentList(commentListBo);

    }

    //这下面有个user没有搞
    @ResponseBody
    @PostMapping("comment/post")
    public BaseRespVo postComment(@RequestBody TopicComment topicComment) {
        Integer userId = 1;
        topicComment.setUserId(userId);
        weChartService.insertComment(topicComment);
        TopicComment result = weChartService.selectCommentById(topicComment.getId());
        return BaseRespVo.getResult(result, "成功", 0);
    }
    //评论里面找有图没图的评论
    @ResponseBody
    @GetMapping("comment/count")
    public BaseRespVo postComment(Integer valueId, Integer type) {
        return commentService.getCountNum(valueId,type);
    }

}